package top.aylan.org.service.impl;

import org.springframework.stereotype.Service;
import top.aylan.org.dao.CategoryMapper;
import top.aylan.org.entity.Category;
import top.aylan.org.entity.CategoryExample;
import top.aylan.org.service.CategoryService;
import top.aylan.org.tools.Pager;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2018/12/24.
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    CategoryMapper categoryMapper;

    @Override
    public List<Category> selectCategoryByPage(Pager pager) throws Exception {

        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        pager.setTotalRow(categoryMapper.countByExample(example));
        example.setOfset((pager.getCurrentPage() - 1)*pager.getEveyPageRow());
        example.setSize(pager.getEveyPageRow());
        return categoryMapper.selectByExampleWithPage(example);
    }

    @Override
    public List<Category> selectParentCategory() throws Exception {
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(0);
        return categoryMapper.selectByExample(example);
    }

    @Override
    public int getCount(CategoryExample example) throws Exception {
        CategoryExample example1 = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        return categoryMapper.countByExample(example);
    }

    @Override
    public Category findById(Integer id) throws Exception {
        return categoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public Category findByExample(CategoryExample example) throws Exception {
        CategoryExample example1 = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        return categoryMapper.selectByExample(example).get(0);
    }

    @Override
    public int delete(Integer id) throws Exception {
        return categoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int save(Category category) throws Exception {
        return categoryMapper.insertSelective(category);
    }

    @Override
    public int updateById(Integer id, Category category) throws Exception {
        category.setId(id);
        return categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public List<Category> selectAllCategory() throws Exception {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andStatusEqualTo((byte)1);
        return categoryMapper.selectByExample(categoryExample);
    }
}
